package com.wyz.demo;

/**
 * 这个是演示简单的父子线程关系，通过手动赋值的方式解决
 */
public class ThreadLocalExample {
    private static final ThreadLocal<String> threadLocal = new ThreadLocal<>();

    public static void main(String[] args) {
        threadLocal.set("Parent Thread Value");

        // 获取父线程的 ThreadLocal 值
        String parentValue = threadLocal.get();

        // 创建子线程并传递父线程的 ThreadLocal 值
        Thread childThread = new Thread(() -> {
            // 在子线程中设置父线程的值
            threadLocal.set(parentValue);
            System.out.println("Child Thread: " + threadLocal.get());
        });

        childThread.start();

        try {
            childThread.join(); // 等待子线程执行完毕
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("Parent Thread: " + threadLocal.get());
    }
}